package zuosheng.stackandQueue;

import java.util.Stack;

/**
 * @author lj
 * @Description: 用递归和栈来逆序一个栈
 * @date 2020-11-14 13:24
 * @QQ 851477108
 */


public class ReverseStack {
    //逆序主函数
    public static void reverse(Stack<Integer> stack){
        if (stack.isEmpty()){
            return;
        }

        int i = getAndRemoveLastElement(stack);
        reverse(stack);
        stack.push(i);
    }

    //将栈底元素返回并且移除
    public static int getAndRemoveLastElement(Stack<Integer> stack){
        int result = stack.pop();
        if (stack.isEmpty()){
            return result;
        }else {
            int last = getAndRemoveLastElement(stack);
            stack.push(result);
            return last;
        }
    }
}
